#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("snake");
//    ui->verticalLayout->
    ui->stackedWidget->addWidget(&gamewin);
    ui->stackedWidget->setCurrentWidget(&gamewin);
    connect(&gamewin, SIGNAL(gameStatusSig(int)), this, SLOT(handleGameStaus(int)));
    connect(&gamewin, &gamewindow::genScoreSig, this, &MainWindow::handleGameScore);
    connect(this, SIGNAL(gamePauseSig()), this, SLOT(handleGamePauseSig()));
    connect(this, &MainWindow::gamePauseSig, &gamewin, &gamewindow::handleGamePause);
    gameStatus = GAME_STATUS_PAUSE;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    event->ignore();
}

void MainWindow::showHome()
{
    this->show();
}


void MainWindow::on_pushButton_clicked()
{
    gamewin.setGameStatus(gameStatus == GAME_STATUS_PAUSE ? GAME_STATUS_RUNNING : GAME_STATUS_PAUSE);
}

void MainWindow::on_pushButton_2_clicked()
{
//    ui->stackedWidget->setCurrentWidget(&descwin);
    emit gamePauseSig();
    descwin.show();
}

void MainWindow::on_pushButton_3_clicked()
{
    QApplication::quit();
}

void MainWindow::handleGameStaus(int status)
{
    qDebug()<<"game status: "<<status;
    gameStatus = status;
    switch(status) {
        case GAME_STATUS_RUNNING: {
            ui->pushButton->setText("暂停游戏");
        }break;
        case GAME_STATUS_PAUSE: {
            ui->pushButton->setText("开始游戏");
        }
    }
}

void MainWindow::handleGameScore(int score)
{
    QString s = "得分：";
    s.append(QString::number(score));
    qDebug()<<"current score: "<<score;
    qDebug()<<s;

    ui->label_score->setText(s);
}

void MainWindow::handleGamePauseSig()
{
    handleGameStaus(GAME_STATUS_PAUSE);
}
